package com.yanqu.road.server.cmd.activity.feast;

import com.yanqu.road.entity.activity.feast.config.ShanHeFeastConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ShanHeFeastProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.feast.ShanHeFeastMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_SHAN_HE_FEAST_GET_RANK_REWARD, desc = "领取榜单奖励")
public class FeastGetRankRewardCmd extends AbstractCommand {

    private ShanHeFeastProto.FeastGetRankRewardReqMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = ShanHeFeastProto.FeastGetRankRewardReqMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ShanHeFeastConfig config = ShanHeFeastMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if (!ActivityMgr.activityInCanRewardPeriod(config.getActivityInfo())) {
            ret = GameErrorCode.E_SHAN_HE_FEAST_NO_OPERATE_TIME;
            return;
        }

        ShanHeFeastProto.CrossFeastGetRankRewardReqMsg.Builder builder = ShanHeFeastProto.CrossFeastGetRankRewardReqMsg.newBuilder();
        builder.setActivityId(config.getActivityInfo().getActivityId());
        builder.setReq(reqMsg.toBuilder());
        player.sendPacket(CrossProtocol.C_CROSS_SHAN_HE_FEAST_GET_RANK_REWARD, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret == 0) {
            return;
        }

        ShanHeFeastProto.FeastGetRankRewardRespMsg.Builder respMsg = ShanHeFeastProto.FeastGetRankRewardRespMsg.newBuilder();
        respMsg.setRet(ret);
        player.sendPacket(ClientProtocol.U_SHAN_HE_FEAST_GET_RANK_REWARD, respMsg);
    }
}
